package repository

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"gorm.io/gorm"
)

var (
	_selectSysOperLogVo = "oper_id, title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location, oper_param, json_result, status, error_msg, oper_time, cost_time"
)

type SysOperLogRepository struct {
	DB *gorm.DB
}

func NewSysOperLogRepository() *SysOperLogRepository {
	return &SysOperLogRepository{
		DB: db.GetMasterDB(),
	}
}
func (r *SysOperLogRepository) GetById(operId int64) (*model.SysOperLog, error) {
	db := r.DB
	m := &model.SysOperLog{}
	err := db.Model(&model.SysOperLog{}).Where("oper_id = ?", operId).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *SysOperLogRepository) List(query *vo.QuerySysOperLogReq, pageReq *request.PageRequest) ([]*model.SysOperLog, int64, error) {
	tx := r.DB.Model(&model.SysOperLog{})
	if query != nil {
		if query.Title != nil {
			tx = tx.Where("title = ?", *query.Title)
		}
		if query.BusinessType != nil {
			tx = tx.Where("business_type = ?", *query.BusinessType)
		}
		if query.Method != nil {
			tx = tx.Where("method = ?", *query.Method)
		}
		if query.RequestMethod != nil {
			tx = tx.Where("request_method = ?", *query.RequestMethod)
		}
		if query.OperatorType != nil {
			tx = tx.Where("operator_type = ?", *query.OperatorType)
		}
		if query.OperName != nil {
			tx = tx.Where("oper_name = ?", *query.OperName)
		}
		if query.DeptName != nil {
			tx = tx.Where("dept_name = ?", *query.DeptName)
		}
		if query.OperUrl != nil {
			tx = tx.Where("oper_url = ?", *query.OperUrl)
		}
		if query.OperIp != nil {
			tx = tx.Where("oper_ip = ?", *query.OperIp)
		}
		if query.OperLocation != nil {
			tx = tx.Where("oper_location = ?", *query.OperLocation)
		}
		if query.Status != nil {
			tx = tx.Where("status = ?", *query.Status)
		}
		if query.ErrorMsg != nil {
			tx = tx.Where("error_msg = ?", *query.ErrorMsg)
		}
		if query.Params != nil {
			val, have := query.Params["beginTime"]
			if have {
				beginTime := val.(string)
				tx = tx.Where("date_format(oper_time,'%Y%m%d') >= date_format('" + beginTime + "','%Y%m%d')")
			}
			val, have = query.Params["endTime"]
			if have {
				endTime := val.(string)
				tx = tx.Where("date_format(oper_time,'%Y%m%d') <= date_format('" + endTime + "','%Y%m%d')")
			}
		}
	}
	list := make([]*model.SysOperLog, 0)
	if pageReq != nil {
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			tx = tx.Select(_selectSysOperLogVo)
			tx = db.BuildPageAndOrder(tx, &query.PageRequest, "oper_time desc")
			if err := tx.Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		tx = tx.Select(_selectSysOperLogVo)
		if err := tx.Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *SysOperLogRepository) Save(m *model.SysOperLog) error {
	return r.DB.Model(&model.SysOperLog{}).Create(m).Error
}

// Update 更新
func (r *SysOperLogRepository) Update(m *model.SysOperLog) error {
	return r.DB.Model(&model.SysOperLog{}).Where("oper_id = ?", m.OperId).Updates(m).Error
}

// Delete 删除
func (r *SysOperLogRepository) Delete(operId int64) error {
	return r.DB.Model(&model.SysOperLog{}).Where("oper_id = ?", operId).Delete(&model.SysOperLog{}).Error
}

// DeleteBatch 批量删除
func (r *SysOperLogRepository) BatchDelete(ids []string) error {
	db := r.DB
	db = db.Where("(oper_id) IN ?", ids)
	return db.Delete(&model.SysOperLog{}).Error
}

func (r *SysOperLogRepository) Clean() error {
	return r.DB.Exec("truncate table sys_oper_log").Error
}
